package com.platform.utils.MU;

import com.platform.utils.MU.android.*;
import com.platform.utils.MU.ios.*;
import org.json.JSONArray;
import org.json.JSONObject;

public class MessagePushUtils {

    private String appKey = "";
    private String appMasterSecret = "";
    private String timestamp = null;
    private PushClient client = new PushClient();


    public MessagePushUtils(String key ,String secret){
        try {
            appKey = key;
            appMasterSecret = secret;
        }catch (Exception e){
            e.printStackTrace();
            System.exit(1);
        }
    }

    /**
     * 安卓广播
     * @param ticker
     * @param title
     * @param text
     * @throws Exception
     */
    public boolean  sendAndroidBroadcast(String ticker,String title,String text) throws Exception{
        AndroidBroadcast broadcast = new AndroidBroadcast(appKey,appMasterSecret);
        broadcast.setTicker(ticker);
        broadcast.setTitle(title);
        broadcast.setText(text);
        broadcast.goAppAfterOpen();
        broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
       // broadcast.setExtraField("test","hello world");
        return client.send(broadcast);
    }

    /**
     * 安卓单播
     * @param deviceToken
     * @param ticker
     * @param title
     * @param text
     * @return
     */
    public boolean sendAndroidUnicast(String deviceToken,String ticker,String title,String text) throws Exception{
        AndroidUnicast unicast = new AndroidUnicast(appKey,appMasterSecret);
        unicast.setDeviceToken(deviceToken);
        unicast.setTicker(ticker);
        unicast.setTitle(title);
        unicast.setText(text);
        unicast.goAppAfterOpen();
        unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        unicast.setProductionMode();
        //unicast.setExtraField("test","hello world");
        return client.send(unicast);
    }

    /**
     * 安卓组播
     * @return
     * @throws Exception
     */
    public boolean sendAndroidGroupcast(String ticker,String title,String text) throws Exception{
        AndroidGroupcast groupcast = new AndroidGroupcast(appKey,appMasterSecret);
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        JSONObject TestTag = new JSONObject();
        testTag.put("tag","test");
        TestTag.put("tag","Test");
        tagArray.put(testTag);
        tagArray.put(TestTag);
        whereJson.put("and",tagArray);
        filterJson.put("where",whereJson);
        System.out.println(filterJson.toString());
        groupcast.setFilter(filterJson);
        groupcast.setTicker(ticker);
        groupcast.setTicker(title);
        groupcast.setText(text);
        groupcast.goAppAfterOpen();
        groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        groupcast.setProductionMode();
        return client.send(groupcast);
    }

    /**
     * 安卓个性化
     * @return
     * @throws Exception
     */
    public boolean sendAndroidCustomizedcast(String ticker,String title,String text) throws Exception{
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appKey,appMasterSecret);
        customizedcast.setAlias("alias", "alias_type");
        customizedcast.setTicker(ticker);
        customizedcast.setTitle(title);
        customizedcast.setText(text);
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        customizedcast.setProductionMode();
        return  client.send(customizedcast);
    }

    /**
     * 安卓文件播测试
     * @throws Exception
     */
    public boolean sendAndroidCustomizedcastFile(String ticker,String title,String text) throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appKey,appMasterSecret);
        String fileId = client.uploadContents(appKey,appMasterSecret,"aa"+"\n"+"bb"+"\n"+"alias");
        customizedcast.setFileId(fileId, "alias_type");
        customizedcast.setTicker(ticker);
        customizedcast.setTitle(title);
        customizedcast.setText(text);
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        customizedcast.setProductionMode();
        return client.send(customizedcast);
    }

    /**
     *
     * @param ticker
     * @param title
     * @param text
     * @throws Exception
     */
    public boolean sendAndroidFilecast(String ticker,String title,String text) throws Exception {
        AndroidFilecast filecast = new AndroidFilecast(appKey,appMasterSecret);
        String fileId = client.uploadContents(appKey,appMasterSecret,"aa"+"\n"+"bb");
        filecast.setFileId( fileId);
        filecast.setTicker(ticker);
        filecast.setTitle(title);
        filecast.setText(text);
        filecast.goAppAfterOpen();
        filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        return client.send(filecast);
    }

    /**
     * IOS广播
     * @throws Exception
     */
    public boolean sendIOSBroadcast(String alert) throws Exception {
        IOSBroadcast broadcast = new IOSBroadcast(appKey,appMasterSecret);
        broadcast.setAlert(alert);
        broadcast.setBadge( 0);
        broadcast.setSound( "default");
        broadcast.setTestMode();
        //broadcast.setCustomizedField("test", "helloworld");
        return client.send(broadcast);
    }


    /**
     * IOS单播
     * @throws Exception
     */
    public boolean sendIOSUnicast(String deviceToken,String alert) throws Exception {
        IOSUnicast unicast = new IOSUnicast(appKey,appMasterSecret);
        unicast.setDeviceToken( deviceToken);
        unicast.setAlert(alert);
        unicast.setBadge( 0);
        unicast.setSound( "default");
        unicast.setTestMode();
        unicast.setCustomizedField("test", "helloworld");
        return client.send(unicast);
    }

    /**
     * IOS组播
     * @return
     * @throws Exception
     */
    public boolean sendIOSGroupcast(String alert) throws Exception {
        IOSGroupcast groupcast = new IOSGroupcast(appKey,appMasterSecret);
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        testTag.put("tag", "iostest");
        tagArray.put(testTag);
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);
        System.out.println(filterJson.toString());
        groupcast.setFilter(filterJson);
        groupcast.setAlert(alert);
        groupcast.setBadge( 0);
        groupcast.setSound( "default");
        groupcast.setTestMode();
        return client.send(groupcast);
    }

    /**
     * IOS个性化测试
     * @throws Exception
     */
    public boolean sendIOSCustomizedcast(String alert) throws Exception {
        IOSCustomizedcast customizedcast = new IOSCustomizedcast(appKey,appMasterSecret);
        customizedcast.setAlias("alias", "alias_type");
        customizedcast.setAlert(alert);
        customizedcast.setBadge( 0);
        customizedcast.setSound( "default");
        customizedcast.setTestMode();
        return client.send(customizedcast);
    }

    /**
     * IOS
     * @throws Exception
     */
    public boolean sendIOSFilecast(String alert) throws Exception {
        IOSFilecast filecast = new IOSFilecast(appKey,appMasterSecret);
        String fileId = client.uploadContents(appKey,appMasterSecret,"aa"+"\n"+"bb");
        filecast.setFileId( fileId);
        filecast.setAlert(alert);
        filecast.setBadge( 0);
        filecast.setSound( "default");
        filecast.setTestMode();
        return client.send(filecast);
    }

}
